package com.gqc.test07.TX;

import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import com.gqc.pojo.User;


public class UserDaoImpl implements UserDao {

	private JdbcTemplate jdbcTemplate;
	
	

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	@Override
	public int add() {
		String sql = "insert into user(name,age) values(?,?)";
		int i = this.jdbcTemplate.update(sql, "rose",18);
		return i;
	}

	@Override
	public int delete() {
		String sql = "delete from user where id=?";
		int i = this.jdbcTemplate.update(sql, 1);
		return i;
	}

	@Override
	public int update() {
		String sql = "update user set name = ? where id=?";
		int i = this.jdbcTemplate.update(sql, "TOM",2);
		return i;
	}

	@Override
	public User findById() {
		String sql = "select * from user where id=?";
		RowMapper<User> rowMapper 
			= new BeanPropertyRowMapper<User>(User.class);
		User User = this.jdbcTemplate.queryForObject(sql, rowMapper,3);
		return User;
	}

	@Override
	public List<User> findUser() {
		String sql = "select * from user";
		RowMapper<User> rowMapper 
			= new BeanPropertyRowMapper<User>(User.class);
		List<User> list = this.jdbcTemplate.query(sql,rowMapper);
		return list;
	}



	@Override
	public void transfer(int outUser, int inUser, int money) {
		// TODO Auto-generated method stub
		
		this.jdbcTemplate.update("update user set age = age + ? "
				+ "where id=?",money,inUser);
//		int i= 1/0;
		this.jdbcTemplate.update("update user set age = age - ? "
				+ "where id=?",money,outUser);
		
		
	}

}
